package com.yzy.fly.mapper;

import com.yzy.fly.entity.core.FlySql;
import com.yzy.fly.kernel.mapper.FlyBasicMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface FlySqlMapper extends FlyBasicMapper<FlySql> {
    @Select("select * from fly_sql a where a.id = #{id}")
    FlySql selectById(@Param("id") int id);

    @Select("select * from fly_sql a where 1=1")
    List<FlySql> selectAll();

    @Select("select * from fly_sql a where 1=1 and a.componentId = #{componentId} order by a.seq asc")
    List<FlySql> selectAllByClassId(@Param("componentId") int componentId);

    @Select("select * from fly_sql a where 1=1 and a.systemId = #{systemId}")
    List<FlySql> selectAllBySystemId(@Param("systemId") int systemId);

//    @Select({"select * from fly_sql a where 1=1 and a.classId = (select b.fieldId from fly_field_annotation b where b.id = #{annotationId})",
//    "","",""})
    @Select({"<script>",
            "select * ",
            "from fly_sql a",
            "where 1=1",
            "<when test='componentId!=null'>",
            "AND a.componentId = #{componentId}",
            "</when>",
            "<when test='systemId!=null'>",
            "AND a.systemId = #{systemId}",
            "</when>",
            "</script>"})
    List<FlySql> selectAllByParentId(@Param("componentId") Integer componentId,@Param("systemId") Integer systemId);

    @Select("select * from fly_sql a where 1=1 and a.name = #{name}")
    FlySql selectOneByName(@Param("name") String name);

//    @Update("update fly_sql a set seq = seq - 1 where id = #{id} and seq = #{seq}")
//    int upById(@Param("id") int id, @Param("seq")int seq);
//
//    @Update("update fly_sql a set seq = seq + 1 where id = #{id} and seq = #{seq}")
//    int downById(@Param("id")int id, @Param("seq")int seq);
//
//    @Update({"<script>",
//            "update fly_sql a set seq = seq - 1  where 1=1 and ",
//            "<choose>",
//            "<when test='parentId == null'>",
//            "componentId is null ",
//            "</when>",
//            "<otherwise>",
//            "componentId = #{parentId} ",
//            "</otherwise>",
//            "</choose>",
//            "and seq = #{seq}",
//            "</script>"})
//    int upByParentId(@Param("parentId") Integer parentId, @Param("seq")int seq);
//
//    @Update({"<script>",
//            "update fly_sql a set seq = seq + 1  where 1=1 and ",
//            "<choose>",
//            "<when test='parentId == null'>",
//            "componentId is null ",
//            "</when>",
//            "<otherwise>",
//            "componentId = #{parentId} ",
//            "</otherwise>",
//            "</choose>",
//            " and seq = #{seq}",
//            "</script>"})
//    int downByParentId(@Param("parentId") Integer parentId, @Param("seq")int seq);
//
//    @Select({"<script>",
//            "select max(a.seq) from fly_sql a where 1=1 and ",
//            "<choose>",
//            "<when test='parentId == null'>",
//            "componentId is null ",
//            "</when>",
//            "<otherwise>",
//            "componentId = #{parentId} ",
//            "</otherwise>",
//            "</choose>",
//            "</script>"})
//    Integer maxSeqNo(@Param("parentId")Integer parentId);




}